package com.aleaho.cftrack.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * Created by Administrator on 2017/10/21.
 */

public class NetUtil {

    /**
     * 检测网络状态是否连通
     * @param context   上下文
     * @return 网络检测结果
     *          <p>
     *              <pre>true:网络可用</pre>
     *              <pre>false:网络不可用</pre>
     *          </p>
     */
    public static boolean isNetWorkAvailable(Context context){

        try{
            ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo info = cm.getActiveNetworkInfo();

            return  null != info && info.isConnected() && info.isAvailable();

        }catch (Exception ex){
            Log.e(Constants.TAG, "当前网络不可用");
            return false;
        }

    }
}
